# -*- coding: utf-8 -*-
# @File  : TwoLinkList_add.py
# @Author: Liuhh
# @Date  : 2017/10/16
# @Desc  : 

#好气啊
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        #事实上，在函数定义时，python中并不需要指定参数的类型，
        #调用时，直接传入即可
        """
        head = ListNode(None)
        p = head
        
        while l1.val != None and l2.val != None:
            data = l1.val + l2.val
            node = ListNode(data)
            p.next = node
            p = p.next
            l1 = l1.next
            l2 = l2.next
            
        while l1.val != None:
            data = l1.val
            node = ListNode(data)
            p.next = node
            p = p.next
            l1 = l1.next
            
        while l2.val != None:
            data = l2.val
            node = ListNode(data)
            p.next = node
            p = p.next
            l2 = l2.next
            
        head = head.next
        return head
 

def List_print(ListNode):
    Node = ListNode
    print(str(ListNode)+':')
    while Node.val != None or Node.next != None:
        print(Node.value,'->')
        Node = Node.next

       
if __name__ == '__main__':
    l1 = [1,3,5,7]
    l2 = [2,4,6,8]
    head1 = ListNode(l1[0])
    p1 = head1
    head2 = ListNode(l2[0])
    p2 = head2
    for x in l1[1:]:
        node = ListNode(x)
        p1.next = node
        p1 = p1.next
    
    for x in l2[1:]:
        node = ListNode(x)
        p2.next = node
        p2 = p2.next
        
    s = Solution()
    out = s.addTwoNumbers(head1, head2)  
    List_print(head1)
    List_print(head2)
    List_print(out)   
        